#!/bin/bash
#

FILE_NAME=/root/lession/5.6/my.conf

function get_all_segments {
  # shellcheck disable=SC2005
  # echo "$(sed -n '/\[.*\]/p' $FILE_NAME | sed -e 's/\[//g' -e 's/\]//g')"

  sed -n '/\[.*\]/p' $FILE_NAME | sed -e 's/\[//g' -e 's/\]//g'
}

function count_items_in_segment {
  # ^$ 代表空行
  items=$(sed -n '/\['"$1"'\]/,/\[.*\]/p' my.conf | grep -v "^#" | grep -v "^$" | grep -v "\[.*\]")

  index=0
  for item in $items
  do
    index=$((index + 1))
  done

  echo "$index"
}

number=0

for seg in $(get_all_segments)
do
  number=$((number + 1))
  item_count=$(count_items_in_segment "$seg") #"$seg": Double quote to prevent globbing and word splitting.
  echo "$number: $seg    $item_count"
done
